package com.atguigu.tingshu.album.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.print.attribute.standard.Media;
import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;

    @Autowired
    private VodUploadClient vodUploadClient;
    /**
     * 上传声音
     * /api/album/trackInfo/uploadTrack
     * @param file  这个是上传的声音
     * @return 声音媒体唯一标识和声音媒体在线地址
     */
    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        Map<String, String> map = null;
        try {
            map = new HashMap<>();
            //创建一个本地文件夹存入流,s是路径
            String s = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
            if (StringUtil.isEmpty(s)){
                throw new GuiguException(400,"上传声音为空");
            }
            //创建请求对象
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(s);
            //上传声音
            VodUploadResponse response = vodUploadClient.upload("ap-guangzhou", request);
            //封装返回
            map.put("mediaFileId",response.getFileId());
            map.put("mediaUrl",response.getMediaUrl());
            log.info("上传声音成功了牛啊");
        } catch (Exception e) {
            log.error("上传声音失败了");
            throw new RuntimeException(e);
        }

        return map;
    }

    /**
     * 查询云点播声音的 时长 ，大小，类型
     * @param mediaFileId 媒体文件的唯一标识
     * @return
     */
    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] id = {mediaFileId};
            req.setFileIds(id);
            // 返回的resp是一个ProcessImageResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);

            if (resp!=null){
                MediaInfo[] medidInfos = resp.getMediaInfoSet();
                if (medidInfos!=null && medidInfos.length>0){
                    MediaInfo medidInfo = medidInfos[0];
                    //获取类型
                    String type = medidInfo.getBasicInfo().getType();
                    //获取声音大小
                    Long size = medidInfo.getMetaData().getSize();
                    //获取时长
                    Float duration = medidInfo.getMetaData().getDuration();

                    trackMediaInfoVo.setType(type);
                    trackMediaInfoVo.setSize(size);
                    trackMediaInfoVo.setDuration(duration);
                }
            }
        } catch (Exception e) {
            throw new GuiguException(400,"云点播信息异常");
        }
        return trackMediaInfoVo;
    }

    /**
     * 云点播声音信息删除
     * @param mediaFileId
     */
    @Override
    public void deleteTrackMedia(String mediaFileId) {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred,vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(mediaFileId);
            // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            DeleteMediaResponse resp = client.DeleteMedia(req);
        } catch (Exception e) {
            throw new GuiguException(400,"删除时出现错误");
        }
    }
}
